package com.easylinkin.linkappapi.personality.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.personality.entity.Personality;
import com.easylinkin.linkappapi.personality.service.PersonalityService;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 个性化配置
 * </p>
 *
 * @author XuGuoQing
 * @since 2020-05-19
 */
@RestController
@RequestMapping("/personality")
public class PersonalityController {

    @Resource
    private PersonalityService personalityService;

    @CommonOperateLogAnnotate(module = LogModule.PERSONALITY, desc = "新增个性化-带租户id")
    @PostMapping("/addPersonality/{tenantId}")
    public RestMessage addPersonality(@RequestBody Personality personality,@PathVariable String tenantId) {
        return RestBuilders.successBuilder().success(personalityService.addPersonality(personality,tenantId)).build();
    }
    
    @CommonOperateLogAnnotate(module = LogModule.PERSONALITY, desc = "新增个性化")
    @PostMapping("/addPersonality")
    public RestMessage addPersonality(@RequestBody Personality personality) {
        return RestBuilders.successBuilder().success(personalityService.addPersonality(personality,null)).build();
    }

    @CommonOperateLogAnnotate(module = LogModule.PERSONALITY, desc = "编辑个性化")
    @PostMapping("/updatePersonality")
    public RestMessage updatePersonality(@RequestBody Personality personality) {
        return RestBuilders.successBuilder().success(personalityService.updatePersonality(personality)).build();
    }

    @CommonOperateLogAnnotate(module = LogModule.PERSONALITY, desc = "删除个性化")
    @PostMapping("/deletePersonalitys")
    public RestMessage deletePersonality(@RequestBody List<String>list) {
        return RestBuilders.successBuilder().success(personalityService.deletePersonality(list)).build();
    }

    @PostMapping("/getPage")
    public RestMessage getPage(@RequestBody RequestModel<Personality> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Personality> record = personalityService.getPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    @PostMapping("/getPersonalIfo")
    public RestMessage getPersonalIfoByDomain(@RequestBody Personality personality) {
        String   domain  =  personality.getDomin();
        Assert.notNull(domain, "域名不能为空");
        Assert.isTrue(domain.length()>0, "域名不能为空");
        personality= personalityService.getPersonalIfoByDomain(domain);
        return RestBuilders.successBuilder().data(personality).build();
    }


    @PostMapping("/getList")
    public RestMessage getList() {
        List <Personality>  personalityList= personalityService.getList();
        return RestBuilders.successBuilder().data(personalityList).build();
    }

    /**
     * 根据租户获取个性化内容
     */
    @RequestMapping("/getPersonalityByTenant/{tenantId}")
    public RestMessage getPersonalityByTenant(@PathVariable String tenantId) {
        return RestBuilders.successBuilder().data(personalityService.getPersonalityByTenant(tenantId)).build();
    }

}
